package com.aaa.system.service.impl;

import com.aaa.entity.User;
import com.aaa.system.dao.DeptDao;
import com.aaa.system.service.IDeptService;
import com.aaa.system.vo.DeptVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aaa.entity.Dept;
import com.aaa.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 部门/科室表 服务实现类
 * </p>
 *
 * @author L
 * @since 2023-03-21
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptDao, Dept> implements IDeptService {

    @Autowired
    private DeptDao deptDao;

    @Override
    public Result<List<Dept>> findAll() {
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        queryWrapper.eq("del_flag",0);
        List<Dept> depts = deptDao.selectList(queryWrapper);
        return new Result<>(200,"查询所有部门",depts);
    }

    @Override
    public Result<IPage<Dept>> selectAll(Integer current, Integer size, DeptVo deptVo) {
        IPage<Dept> page = new Page<>(current,size);
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(deptVo.getDeptName())){
            queryWrapper.like("dept_name",deptVo.getDeptName());
        }
        if (Objects.nonNull(deptVo.getStatus())){
            queryWrapper.eq("status",deptVo.getStatus());
        }
        if (Objects.nonNull(deptVo.getDateRange()) && deptVo.getDateRange().length==2){
            queryWrapper.between("create_time",deptVo.getDateRange()[0],deptVo.getDateRange()[1]);
        }
        queryWrapper.eq("del_flag","0");
        IPage<Dept> deptIPage = deptDao.selectPage(page,queryWrapper);
        return new Result<>(200,"查询科室成功",deptIPage);
    }

    @Override
    public Result<ArrayList<Long>> muldelDept(Long[] deptIds) {
        //循环遍历部门id，拥有用户的部门不能删除
        //可以删除的部门
        ArrayList<Long> okList = new ArrayList<>();
        //不可以删除的部门
        ArrayList<Long> noList = new ArrayList<>();
        String msg = "";
        for (Long deptId : deptIds) {
            List<User> users =  deptDao.selectByDeptId(deptId);
            if (users.size()>0){
                //如果部门下有用户，不可以进行删除
                noList.add(deptId);
                msg="批量删除失败";
            }else {
                //如果部门下没有用户，可以直接进行删除
                okList.add(deptId);
                msg="批量删除成功";
            }
        }
        //当这些部门都可以删除时，再执行删除操作
        if (noList.size()==0){
            boolean b = deptDao.muldelDept(okList);
        }
        return new Result<ArrayList<Long>>(200,msg,noList);
    }

    @Override
    public Result<Boolean> saveOrUpdateDept(Dept dept) {
        //当对象中有dept_id时就是修改，没有id的时候就是添加
        if (Objects.nonNull(dept.getDeptId())){
            dept.setUpdateTime(LocalDateTime.now());
            int i = deptDao.updateById(dept);
            return new Result<Boolean>(200,"修改成功",i>0?true:false);
        }else {
            dept.setCreateTime(LocalDateTime.now());
            int insert = deptDao.insert(dept);
            return new Result<Boolean>(200,"添加成功",insert>0?true:false);
        }
    }

    @Override
    public Result<Long[]> delDept(Long deptId) {
        //不可以删除的部门
        Long[] noList = new Long[1];
        List<User> users =  deptDao.selectByDeptId(deptId);
        //如果当前部门下有用户存在，那么该部门不能删除
        if (users.size()>0){
            noList[0]=deptId;
            return new Result<Long[]>(200,"删除失败",noList);
        }else {
            deptDao.delDeptFlag(deptId);
            return new Result<>(200,"删除成功",null);
        }
    }

    @Override
    public Dept getDeptById(Long deptId) {
        Dept dept = deptDao.selectById(deptId);
        return dept;
    }



}
